package fes.util;

import com.ht.aems.util.RtDataBuffer;
import com.ht.expression.Calculator;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-8-10
 */
public class ExpCalculator {
    RtDataBuffer dataBuffer;
    NumberFormat format = new DecimalFormat(".###");

    public ExpCalculator() {
    }

    public ExpCalculator(RtDataBuffer rtDataQuery) {
        this.dataBuffer = rtDataQuery;
    }

    public static boolean check(String exp) {
        String s = exp.trim();
        int currentIndex = 0;
        currentIndex = s.indexOf("$", currentIndex);
        while(currentIndex != -1) {
            int bracketEnd = s.indexOf(")", currentIndex);
            String temp = s.substring(0, currentIndex);
            temp += "1.0";
            temp += s.substring(bracketEnd + 1, s.length());
            s = temp;
            currentIndex = s.indexOf("$", currentIndex);
        }

        currentIndex = 0;
        currentIndex = s.indexOf("minus", currentIndex);
        while(currentIndex != -1) {
            int bracketStart = s.indexOf("(", currentIndex);
            int bracketEnd = s.indexOf(")", currentIndex);
            int nId = Integer.parseInt(s.substring(bracketStart + 1, bracketEnd));
            String temp = s.substring(0, currentIndex);
            temp += "(" + "0" + "-" + nId + ")";
            temp += s.substring(bracketEnd + 1, s.length());
            s = temp;
            currentIndex = s.indexOf("minus", currentIndex);
        }
        try {
            Calculator calculator = new Calculator();
            calculator.eval(s);
        } catch(Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public Number calculate(String exp) {
        String s = exp.trim();
        Number result;
        int currentIndex = 0;
        currentIndex = s.indexOf("$", currentIndex);
        while(currentIndex != -1) {
            int bracketStart = s.indexOf("(", currentIndex);
            int bracketEnd = s.indexOf(")", currentIndex);
            String sId = s.substring(bracketStart + 1, bracketEnd);
            if(dataBuffer == null)
                return null;
            Double data = (Double) dataBuffer.get(sId + "_2"); //todo: wrong
            if(data == null) data = (Double) dataBuffer.get(sId + "_2_fes");//todo: wrong
            if(data != null) {
                String temp = s.substring(0, currentIndex);
                //if(data > 0)
                temp += format.format(Math.abs(data));//todo: wrong
                //else
                //    temp += "(0 - " + Math.abs(data) + ")";
                temp += s.substring(bracketEnd + 1, s.length());
                s = temp;
            } else
                return null;
            currentIndex = s.indexOf("$", currentIndex);
        }

        currentIndex = 0;
        currentIndex = s.indexOf("minus", currentIndex);
        while(currentIndex != -1) {
            int bracketStart = s.indexOf("(", currentIndex);
            int bracketEnd = s.indexOf(")", currentIndex);
            String sId = s.substring(bracketStart + 1, bracketEnd);
            String temp = s.substring(0, currentIndex);
            temp += "(" + "0" + "-" + sId + ")";
            temp += s.substring(bracketEnd + 1, s.length());
            s = temp;
            currentIndex = s.indexOf("minus", currentIndex);
        }
        try {
            Calculator calculator = new Calculator();
            result = calculator.eval(s);
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    public RtDataBuffer getDataBuffer() {
        return dataBuffer;
    }

    public void setDataBuffer(RtDataBuffer dataBuffer) {
        this.dataBuffer = dataBuffer;
    }
}
